package com.enhe.gck.utils;


import lombok.Data;

import java.math.BigDecimal;

@Data
public class ToChineseUpperCaseUtil {

    static String[] units = {"", "拾", "佰", "仟", "万", "拾万", "佰万", "仟万", "亿", "拾亿", "佰亿", "仟亿", "万亿"};
    static char[] numArray = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("30.01");
        double value = bigDecimal.doubleValue();
        System.out.println("您输入的金额（大写）为：" + formatDecimal(value));
    }


    /**
     * 将整型数字转换为中文
     *
     * @param num 整型数字
     * @return
     */
    public static String formatInteger(int num) {
        char[] val = String.valueOf(num).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            String m = val[i] + "";
            int n = Integer.parseInt(m);
            boolean isZero = n == 0;
            String unit = units[(len - 1) - i];
            if (isZero) {
                if (i > 1 && '0' == val[i - 1]) {
                    continue;
                } else {
                    sb.append(numArray[n]);
                }
            } else {
                sb.append(numArray[n]);
                sb.append(unit);
            }
        }
        return sb.toString();
    }

    /**
     * 将数字转换为中文
     *
     * @param decimal 带小数位的数字
     * @return
     */
    public static String formatDecimal(double decimal) {
        String decimals = String.valueOf(decimal);
        int decIndex = decimals.indexOf(".");
        int integ = Integer.parseInt(decimals.substring(0, decIndex));
        String dec = decimals.substring(decIndex + 1);
        return formatInteger(integ) + "点" + test(dec);
    }


    /**
     * 将小数位数字转换为中文
     *
     * @param decimal 小数位数字
     * @return
     */
    private static String test(String decimal) {
        char[] val = decimal.toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int n = Integer.parseInt(val[i] + "");
            sb.append(numArray[n]);
        }
        return sb.toString();
    }

    /**
     * 将小数位数字转换为中文
     *
     * @param decimal 小数位数字
     * @return
     */
    private static String formatFractionalPart(int decimal) {
        char[] val = String.valueOf(decimal).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int n = Integer.parseInt(val[i] + "");
            sb.append(numArray[n]);
        }
        return sb.toString();
    }

}
